home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / can.zip / CAN.C next >
C/C++ Source or Header  |  1994-02-16  |  2KB  |  89 lines

  1. /*
  2.  * CAN - a program for providing "CANned" arguments to COM files.
  3.  *
  4.  * Copyright 1990 Dave Dunfield.
  5.  *
  6.  * May be freely distributed and used as long as
  7.  * my copyright notices are retained.
  8.  */
  9. #include \mc\stdio.h
  10. #include \mc\file.h
  11.  
  12. #define BUFFER_SIZE    1024
  13.  
  14.     unsigned size;
  15.     char buffer[BUFFER_SIZE], save_code[3];
  16.     FILE *fpr, *fpw;
  17.  
  18.     extern char CAN_S[], CAN_E[];    /* Embedded function pointers */
  19.  
  20. /*
  21.  * Write a binary character to a file
  22.  */
  23. write_byte(c)
  24.     char c;
  25. {
  26.     write(&c, 1, fpw);
  27. }
  28.  
  29. /*
  30.  * Main program - Patch the COM file to include the embedded function.
  31.  */
  32. main(argc, argv)
  33.     int argc;
  34.     char *argv[];
  35. {
  36.     unsigned i;
  37.     char *ptr;
  38.  
  39.     if(argc < 4)
  40.         abort("\nUse: CAN oldfile newfile args...\n\nCopyright 1990 Dave Dunfield\nFreely Distributable.\n");
  41.  
  42.     if(!(fpr = open(argv[1], F_READ)))
  43.         abort("Cannot open INPUT file");
  44.  
  45.     if(!(fpw = open(argv[2], F_WRITE)))
  46.         abort("Cannot open OUTPUT file");
  47.  
  48. /* Determine the size of the file & save the startup code */
  49.     size = i = read(buffer, BUFFER_SIZE, fpr);
  50.     memcpy(save_code, buffer, 3);
  51.     while(i == BUFFER_SIZE)
  52.         size += (i = read(buffer, BUFFER_SIZE, fpr));
  53.  
  54. /* Write out the new startup code that jumps to our embedded function */
  55.     i = size - 3;
  56.     write_byte(0xE9);
  57.     write_byte(i & 255);
  58.     write_byte(i / 256);
  59.  
  60. /* Write out the remainder of the original file */
  61.     lrewind(fpr);
  62.     i = read(buffer, BUFFER_SIZE, fpr);
  63.     write(&buffer[3], i - 3, fpw);
  64.     while(i == BUFFER_SIZE) {
  65.         i = read(buffer, BUFFER_SIZE, fpr);
  66.         write(buffer, i, fpw); }
  67.  
  68. /* Append the embedded function */
  69.     size += (i = CAN_E - CAN_S) + 0x103;
  70.     write_byte(0xBE);    /* Load SI with address */
  71.     write_byte(size & 255);
  72.     write_byte(size / 256);
  73.     write(CAN_S, i, fpw);
  74.  
  75. /* Append the argument extensions */
  76.     for(i=3; i < argc; ++i) {
  77.         write_byte(' ');
  78.         ptr = argv[i];
  79.         while(*ptr)
  80.             write_byte(*ptr++); }
  81.     write_byte('\r');
  82.  
  83. /* Append the original startup code so that can be restored */
  84.     write(save_code, 3, fpw);
  85.  
  86.     close(fpr);
  87.     close(fpw);
  88. }
  89.